

$(function () {
    $.ajax({
        type: "get",
        url: ctx + "/admin/studentEvent/listStudentEvent",
        dataType: "json",
        success: function (wechatReturn) {
            var eMap = wechatReturn.data;
            initEvent(eMap);
        },
        error: function (wechatReturn) {
            $.alert("获取事件失败！");
        }
    });

});

function initEvent(eMap) {
    var tableEventLatest = document.getElementById("tab-event-latest");
    var tableEventUnattend = document.getElementById("tab-event-unattend");
    var tableEventAttended = document.getElementById("tab-event-attended");
    var latestEvent = eMap["latestEvent"];
    var attendedEvent = eMap["attendedEvent"];
    var unAttendEvent = eMap["unattendEvent"];
    var unAttendEventIds = [];

    $.each(unAttendEvent, function (i, e) {
        unAttendEventIds.push(e.id);
    });

    for(var i=0; i<latestEvent.length; i++){
        var e = latestEvent[i];

        var panelDiv = document.createElement("div");

        panelDiv.setAttribute("class", "weui-panel");
        var posterDiv = document.createElement("div");
        posterDiv.setAttribute("class", "poster");
        var posterTextDiv = document.createElement("div");
        posterTextDiv.setAttribute("class", "poster_text");
        var imageA = document.createElement("a");
        imageA.setAttribute("href", e.wxNewsUrl);
        var img = document.createElement("img");
        img.setAttribute("class", "poster_image");
        img.setAttribute("src", ctx + "/resources/wechat/upload/images/" + e.snapshoot);
        var textP = document.createElement("p");
        textP.innerHTML = e.name;

        var textA = document.createElement("a");
        textA.setAttribute("href", "javascript:void(0);");
        textA.setAttribute("class", "weui-btn weui-btn_mini weui-btn_primary");
        textA.setAttribute("id", "event" + e.id);
        textA.innerHTML = "参加";
        if($.inArray(e.id, unAttendEventIds)==-1){
            textA.setAttribute("onclick", "attendEvent(" + e.id + " )");
        }else{
            textA.setAttribute("class", "weui-btn weui-btn_mini weui-btn_primary weui-btn_disabled");
        }

        posterTextDiv.appendChild(textP);
        posterTextDiv.appendChild(textA);

        imageA.appendChild(img);
        posterDiv.appendChild(imageA);
        panelDiv.appendChild(posterDiv);
        panelDiv.appendChild(posterTextDiv);
        tableEventLatest.appendChild(panelDiv);
    }

    for(var i=0; i<unAttendEvent.length; i++){
        var e = unAttendEvent[i];

        var panelDiv = document.createElement("div");

        panelDiv.setAttribute("class", "weui-panel");
        var posterDiv = document.createElement("div");
        posterDiv.setAttribute("class", "poster");
        var posterTextDiv = document.createElement("div");
        posterTextDiv.setAttribute("class", "poster_text");
        var imageA = document.createElement("a");
        imageA.setAttribute("href", e.wxNewsUrl);
        var img = document.createElement("img");
        img.setAttribute("class", "poster_image");
        img.setAttribute("src", ctx + "/resources/wechat/upload/images/" + e.snapshoot);
        var textP = document.createElement("p");
        textP.innerHTML = e.name;
        posterTextDiv.appendChild(textP);
        imageA.appendChild(img);
        posterDiv.appendChild(imageA);
        panelDiv.appendChild(posterDiv);
        panelDiv.appendChild(posterTextDiv);
        tableEventUnattend.appendChild(panelDiv);
    }

    for(var i=0; i<attendedEvent.length; i++){
        var e = attendedEvent[i];

        var panelDiv = document.createElement("div");

        panelDiv.setAttribute("class", "weui-panel");

        var posterTextDiv = document.createElement("div");
        posterTextDiv.setAttribute("class", "poster_text");

        var textA = document.createElement("a");
        textA.innerHTML = e.name;
        textA.setAttribute("href", e.wxNewsUrl);

        var textP = document.createElement("p");
        var date = new Date(e.eventDate);
        textP.innerHTML = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();

        posterTextDiv.appendChild(textA);
        posterTextDiv.appendChild(textP);

        panelDiv.appendChild(posterTextDiv);
        tableEventAttended.appendChild(panelDiv);
    }
}

function attendEvent(eventId){
    $.alert("确定要参加该活动？", "提示", function() {
        $.ajax({
            type: "get",
            url: ctx + "/admin/studentEvent/enroll/" + eventId,
            dataType: "json",
            success: function (wechatReturn) {
                var attendBtn = $("#event" + eventId);
                attendBtn.addClass("weui-btn_disabled");
                attendBtn.removeAttr("onclick");
                $.alert("参加活动成功！");
            },
            error: function (wechatReturn) {
                $.alert("参加活动失败！");
            }
        });
    });
}